#include <bits/stdc++.h>
using namespace std;

// 定义物品结构体
struct Item
{
    int weight;
    int value;
    double ratio;
};

// 比较函数，用于按单位重量的价值排序
bool compareItems(const Item &a, const Item &b)
{
    return a.ratio > b.ratio;
}

// 定义函数计算最大价值
double fractionalKanpsack(int capacity, Item items[], int itemCount)
{
    double totalValue = 0.0;
    int currentWeight = 0; // 当前背包重量
    for (int i = 0; i < itemCount; ++i)
    {
        if (currentWeight + items[i].weight <= capacity)
        {
            // 当前物品可以完全放入背包
            totalValue += items[i].value;
            currentWeight += items[i].weight;
        }
        else
        {
            // 不能完全放入背包则只取一部分放入
            int remainingCapacity = capacity - currentWeight;
            totalValue += items[i].ratio * remainingCapacity;
            break; // 背包已满，停止放入
        }
    }
    return totalValue;
}

int main()
{
    int m, n;
    cin >> m >> n;
    Item items[n];
    // 输入每个物品的重量
    for (int i = 0; i < n; ++i)
    {
        cin >> items[i].weight;
    }

    // 输入每个物品的价值
    for (int i = 0; i < n; ++i)
    {
        cin >> items[i].value;
    }

    // 计算每个物品的单位重量价值
    for (int i = 0; i < n; ++i)
    {
        items[i].ratio = (double)items[i].value / items[i].weight;
    }

    // 按单位重量的价值降序排列
    sort(items, items + n, compareItems);

    // 打印排序后的物品信息
    cout << "Sort items by value-to-weight ratio:" << endl;
    for (int i = 0; i < n; ++i)
    {
        cout << "Item" << i + 1 << ":weight = " << items[i].weight
             << ",value = " << items[i].value
             << ",radio = " << items[i].ratio << endl;
    }

    // 计算最大价值
    double max_value = fractionalKanpsack(m, items, n);

    // 输出结果
    cout << "Maximum value in knapsack = " << max_value << endl;
    return 0;
}